<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>定时任务</el-breadcrumb-item>
        <el-breadcrumb-item>任务列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
          <el-row :gutter="24">
              <el-col :span="5">
                  <el-input size="small" clearable placeholder="输入任务名称" v-model="queryParams.taskName" /> 
              </el-col>
               <el-col :span="5">
                  <el-input size="small" clearable placeholder="输入服务名称" v-model="queryParams.serverName" /> 
              </el-col>
              <el-col :span="2" >
                  <el-button size="small" type="success" @click="queryPage" icon="el-icon-search" v-if="permissionStrValue.indexOf('taskList:query') !== -1">查询</el-button>
              </el-col>
              <el-col :span="2" >
                  <el-button size="small" type="primary" @click="add" icon="el-icon-plus" v-if="permissionStrValue.indexOf('taskList:add') !== -1">新增</el-button>
              </el-col>
          </el-row>
            <!--放表格-->
        <el-table :data="list" border >
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column label="任务名称">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.taskName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="所属的服务">
                 <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.serverName }}</span>
                </template>
            </el-table-column>
            <el-table-column label="cron表达式">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.coron}}</span>
                </template>
            </el-table-column>
            <el-table-column label="接口路径">
                <template slot-scope="scope">
                    <span style="color:#67C23A" size="medium">{{ scope.row.interfaceUrl}}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" width="200px">
                 <template slot-scope="scope">
                     <el-button type="warning" size="small" @click="execute(scope.row.interfaceUrl)" icon="el-icon-view" v-if="permissionStrValue.indexOf('taskList:action') !== -1">执行</el-button>
                     <el-button type="danger" size="small" @click="removeById(scope.row.id)" icon="el-icon-delete" v-if="permissionStrValue.indexOf('taskList:delete') !== -1">删除</el-button>
                 </template>
            </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryParams.currPage"
          :page-sizes="[10,20, 30, 40, 50]"
          :page-size="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>

        <el-dialog
       title="新增数据"
       :visible.sync="addFlag"
       width="40%"
       hegiht="60%"
       @close="clearForm()">
       <el-form :model="messageObj" label-width="110px" ref="messageObj" label-position="left" :rules="messageObjRules">
         <el-form-item label="任务名称:" prop="taskName">
           <el-input v-model="messageObj.taskName" size="small" ></el-input>
         </el-form-item>
         <el-form-item label="资讯内容:" prop="coron">
           <el-input v-model="messageObj.coron" size="small" ></el-input>
         </el-form-item>
          <el-form-item label="微服务名称:" prop="serverName">
           <el-input v-model="messageObj.serverName" size="small" ></el-input>
         </el-form-item>
          <el-form-item label="接口地址:" prop="interfaceUrl">
           <el-input v-model="messageObj.interfaceUrl" size="small" ></el-input>
         </el-form-item>
      
       </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addFlag = false">取 消</el-button>
          <el-button type="primary" @click="addData()">确 定</el-button>
       </span>
     </el-dialog>


      </el-card>

    </div>
</template>
<script>
export default {
    name: "TaskList",
    data() {
        return{
            queryParams: {
                taskName: '',
                serverName: '',
                currPage: 1,
                pageSize: 10
            },
            total: 0,
            list: [],
            permissionStrValue: '',
            messageObj: {
                taskName: '',
                coron: '',
                serverName: '',
                interfaceUrl: '',
            },
            messageObjRules: {
                taskName: [
                    { required: true, message: '请输入任务名称', trigger: 'blur' }
               ],
               coron: [
                   { required: true, message: '请输入cron表达式', trigger: 'blur' }
               ],
               serverName: [
                   { required: true, message: '请输入微服务名称', trigger: 'blur' }
               ],
               interfaceUrl: [
                   { required: true, message: '请输入接口路径', trigger: 'blur' }
               ]

            },
            addFlag: false,

        }
    },
    created() {
        this.queryPage();
        //获取权限值
       this.permissionStrValue = JSON.parse(window.sessionStorage.getItem('permissionList')).toString();


    },
    methods: {
        async queryPage() {
             const{data: res} = await this.$http.post('/user/task-log/query-page', JSON.stringify(this.queryParams), {
                 headers: {
                        'Content-Type': 'application/json;charset=UTF-8',
                }
            })
            if(!res.success) {
                return this.$message.error(res.message);
            }
            console.log(res)
            this.list = res.data;
            this.total = res.total;

        },
        async execute(url) {
            const{data: res} = await this.$http.get(url);
            if(res.success) {
                this.$message.success("执行成功！");
            } else{
                this.$message.error(res.message);
            }

        },
        add() {
            this.addFlag = true;
        },
        async addData() {
            this.$refs.messageObj.validate(async valid => {
                if(valid) {
                    const {data: res} = await this.$http.post('/user/task/add', JSON.stringify(this.queryParams), {
                        headers: {
                        'Content-Type': 'application/json;charset=UTF-8',
                       }
                    });
                    if(res.success) {
                        this.$message.success("增加成功！");
                        this.addFlag = false;
                        this.clearForm();
                        this.queryPage();
                    } else{
                        this.$message.error(res.message);
                    }
                   
                }
            })

        },

        handleSizeChange(newPageSize) {
            this.queryParams.pageSize = newPageSize;
            this.queryPage();
        },
        handleCurrentChange(newPage) {
            this.queryParams.currPage = newPage;
            this.queryPage();

        },
        clearForm(){
            this.messageObj.taskName = '';
            this.messageObj.coron = '';
            this.messageObj.serverName = '';
            this.messageObj.interfaceUrl = [];

        },
        init() {
            this.queryPage.taskName = '';
            this.queryPage.serverName = '';
            this.queryPage.currPage = 1,
            this.queryPage.pageSize = 10
        },
        async removeById(id) {
             const confirmResult = await this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).catch(err => {
                return err;
            });
            //当用户确认时，则返回值为字符串：confirm
            //取消时，返回字符串cancel
            if (confirmResult !== 'confirm'){
                return this.$message.info("已取消删除")
            }
            //发送请求
            const{data: res} = await this.$http.delete('/user/task/delete-by-id/' + id);
            if(res.success) {
                this.$message.success("删除成功！");
                this.init();
                this.queryPage();
            } else {
                this.$message.error(res.message);
            }
        },

    }
}
</script>
<style scoped>

</style>